package com.telecomyt.jwportal.activity.conversation.fragment;


import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.activity.conversation.ConversationDetailsActivity;
import com.telecomyt.jwportal.activity.conversation.adapter.ConversationStatusAdapter;
import com.telecomyt.jwportal.activity.conversation.bean.CallInfoBean;
import com.telecomyt.jwportal.base.LazyBaseFragment;
import com.telecomyt.jwportal.dialog.KeyBoardDialog;
import com.telecomyt.jwportal.utils.ContactsMsgUtils;
import com.yanzhenjie.permission.AndPermission;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author created by chengDa
 * @time 2020/9/9  10:17
 * @description
 */
public class ConversationStatusFragment extends LazyBaseFragment {
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mIvCall)
    ImageView mIvCall;
    @BindView(R.id.mRelativeLayout)
    RelativeLayout mRelativeLayout;
    private String TAG = "ConversationStatusFragment: ";
    Unbinder unbinder;
    private int curPos;
    private ConversationStatusAdapter conversationStatusAdapter;
    private String[] PERMISSIONS = {Manifest.permission.CALL_PHONE};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_conversation_state, null);
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        conversationStatusAdapter = new ConversationStatusAdapter(R.layout.fragment_conversation_stutas_items);
        mRecyclerView.setAdapter(conversationStatusAdapter);
        conversationStatusAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ConversationDetailsActivity.start(getActivity(), conversationStatusAdapter.getData().get(position));
            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            //更新界面数据，如果数据还在下载中，就显示加载框
            Bundle bundle = getArguments();
            curPos = bundle.getInt("position");
            //去服务器下载数据
            if (curPos == 0) {
                List<CallInfoBean> callLog = ContactsMsgUtils.getCallLog(getActivity());
                Collections.reverse(callLog);
                conversationStatusAdapter.setNewData(callLog);
            }
        }
    }


    @OnClick(R.id.mIvCall)
    public void onViewClicked() {
        KeyBoardDialog keyBoardDialog = new KeyBoardDialog(getActivity(), new KeyBoardDialog.onCallClickListener() {
            @Override
            public void onCall(String number) {
                AndPermission.with(getActivity())
                        .runtime()
                        .permission(PERMISSIONS)
                        .onGranted(permissions -> {
                            callPhone(number);
                        })
                        .onDenied(permissions -> {
                            Toast.makeText(getActivity(), "权限取消", Toast.LENGTH_SHORT).show();
                        })
                        .start();
            }
        });
        keyBoardDialog.show();
    }

    /**
     * 拨打电话（直接拨打电话）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
